$(function () {
    //表格
    var classroom_datagrid = $("#classroom_datagrid");
    //弹窗
    var classroom_dialog = $("#classroom_dialog");
    //表单
    var classroom_add_form = $("#classroom_add_form");
    //菜单
    var classroom_menu = $("#classroom_menu");
    //查看教室
    var watch_classroom_dialog = $("#watch_classroom_dialog");
    //查看教室表格
    var watch_classroom_datagrid = $("#watch_classroom_datagrid");
    //停用按钮
    var stop_button = $("#stop_button");
    classroom_datagrid.datagrid({
        url: "/classroom/list",
        columns: [[
            {field: 'name', title: '教室名称', width: 100},
            {field: 'address', title: '教室地址', width: 100},
            {field: 'seats', title: '座位数', width: 100},
            {field: 'remark', title: '教室标语', width: 100},
            {field: 'status', title: '教室状态', width: 100,formatter:formatterStatus}
        ]],
        fit: true,
        fitColumns: true,
        border: false,
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        toolbar: "#classroom_datagrid_toolbar",
        onSelect: function (index, data) {
            if (data.status) {
                /*btn_changeState.linkbutton("disable");*/
                //选中判断教室状态设置按钮文本显示报废还是恢复
                stop_button.linkbutton({
                    text: "启用"
                });
                //移除data-cmd属性,同时增加disable方法禁用按钮
                $("a[data-cmd='editClassroom']").data("cmd", "");
                $("a[data-cmd='editClassroom']").linkbutton("disable");

                //同上,禁止查看
                $("a[data-cmd='watchClassroom']").data("cmd", "");
                $("a[data-cmd='watchClassroom']").linkbutton("disable");
            } else {
                //btn_changeState.linkbutton("enable");
                stop_button.linkbutton({
                    text: "停用"
                });
                //启用编辑
                $("a[data-cmd='editClassroom']").data("cmd", "editClassroom");
                $("a[data-cmd='editClassroom']").linkbutton("enable");
                //启用查看
                $("a[data-cmd='watchClassroom']").data("cmd", "watchClassroom");
                $("a[data-cmd='watchClassroom']").linkbutton("enable");
            }
        }
    });
    //把方法绑定到一个对象上
    var cmdObj = {
        //添加
        addClassroom: function () {
            //清除表单
            classroom_add_form.form("clear");
            classroom_dialog.dialog("setTitle", "添加");
            classroom_dialog.dialog("open");
        },
        //编辑
        editClassroom: function () {
            //获取选中的行
            var selectedRow = classroom_datagrid.datagrid("getSelected");
            if (!selectedRow) {
                $.messager.alert("温馨提示", "你好,请选择一个教室!", "error");
                return;
            }
            //表单的回显
            classroom_add_form.form("load", selectedRow);
            classroom_dialog.dialog("setTitle", "编辑");
            classroom_dialog.dialog("open");
        },
        //停用/启用
        removeClassroom: function () {
            //获取选中的行
            var selectedRow = classroom_datagrid.datagrid("getSelected");
            if (!selectedRow) {
                $.messager.alert("温馨提示", "你好,请选择一个教室!", "error");
            }
            var status = selectedRow.status ? "启用" : "停用";
            var flag = selectedRow.status ? 1 : 0;

            $.messager.confirm("确认窗口", "确认要该教室" + status + "么?", function (yes) {
                if (yes) {
                    var id = selectedRow.id;
                    $.get("/classroom/changeStatus", {id: id, flag: flag}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "info", function () {
                                cmdObj.reloadClassroom();
                            });
                        } else {
                            $.messager.alert("温馨提示", "操作成功", "info");
                        }
                    });
                }
            });
        },
        //确认
        save: function () {
            var url = "/classroom/save";
            var id = $("#classroom_id").val();
            if(id){
                url = "/classroom/update";
            }
            classroom_add_form.form("submit", {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", "操作成功", "info", function () {
                            cmdObj.cancel();
                            cmdObj.reloadClassroom();
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "error");
                    }
                }
            });
        },
        //刷新
        reloadClassroom: function () {
            classroom_datagrid.datagrid("reload");
        },
        //查看
        watchClassroom:function () {
            //获取选中的行
            var selectedRow = classroom_datagrid.datagrid("getSelected");
            if (!selectedRow) {
                $.messager.alert("温馨提示", "你好,请选择一个教室!", "error");
                return;
            }
            var classroomId = selectedRow.id;
            console.log(classroomId);
            //加载当前教室使用班级的信息
            watch_classroom_datagrid.datagrid({
                fit: true,
                fitColumns: true,
                url: "/classgrade/queryByClassroomId?id="+classroomId,
                //url:"/classgrade/list",
                columns: [[
                    {field: 'id', title: '班级编号', width: 100},
                    {field: 'name', title: '班级名称', width: 100},
                    {field: 'stuNumber', title: '学生数量', width: 100},
                    {field: 'college', title: '所在学院', width: 100,formatter:formatterCollege},
                    {field: 'status', title: '班级状态', width: 100,formatter:formatterStatus},
                    {field: 'classroom', title: '所在教室', width: 100,formatter:formatterClassroom},
                    {field: 'employee', title: '班主任', width: 100,formatter:formatterEmployee}
                ]]
            });
            watch_classroom_dialog.dialog("open");
        },
        //取消
        cancel: function () {
            classroom_dialog.dialog("close");
        },
        //高级查询
        search:function () {
            //1.获取查询input里面的值
            var keyword = $("#keyword").val();
            //2.调用datagrid重新加载数据,并且带上查询条件  keyword
            classroom_datagrid.datagrid("load",{keyword:keyword});
        },
        //查看教室确定按钮
        ok:function () {
            watch_classroom_dialog.dialog("close");
        }
    }
    //弹窗事件
    classroom_dialog.dialog({
        buttons: "#classroom_dialog_buttons",
        closed: true
    });
    //查看教室的弹窗
    watch_classroom_dialog.dialog({
        title:"当前教室使用班级信息",
        buttons:"#watch_classroom_dialog_buttons",
        closed:true,
        width:600,
        height:180
    });
    //2.按钮的点击事件通过js来绑定
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        //cmd = "addEmp";
        //cmdObj[cmd]  :根据cmd的值,找到 cmdObj 对应的方法
        //cmdObj[cmd]() : () ,触发方法的执行
        //console.log("..cmd:" + cmd);
        cmdObj[cmd]();
    });
});
//格式化状态
function formatterStatus(value,row,index) {
    return (value == 0) ? "<span style='color: green'>已启用</span>" : "<span style='color: red'>已停用</span>";
}
//格式化学院
function formatterCollege(value,row,index) {
    return value ? value.name : "";
}
//格式化状态
function formatterStatus(value,row,index) {
    if(value == 0){
        return "<font color='green'>已启用</font>";
    }
    if(value == 1){
        return "<font color='red'>已停用</font>";
    }
}
//格式化教室
function formatterClassroom(value,row,index) {
    return value ? value.name : "";
}
//格式化班主任
function formatterEmployee(value,row,index) {
    return value ? value.realName : "<font color='red'>尚未分配班主任</font>";
}